﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using VirtualPress.Tool.Framework;
using VirtualPress.Infrastructure.Entities.Entity;

namespace VirtualPress.Portal.WebLogic
{
    [AttributeUsage(AttributeTargets.Method)]
    public class SelfArticleAttribute : FilterAttribute, IActionFilter
    {
        private string ParamName { get; set; }
        private bool isGet { get; set; }
        public SelfArticleAttribute(string paramName, bool isGet=true)
        {
            this.ParamName = paramName;
            this.isGet = isGet;
        }
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string articleId=string.Empty;
            if (isGet)
            {
                if (ParamName == "id")
                {
                    string url = filterContext.HttpContext.Request.RawUrl;
                    if (filterContext.HttpContext.Request.RawUrl.IndexOf('?') > 0)
                    {
                        url = url.Substring(0, url.IndexOf('?'));
                    }
                    articleId = url.Substring(url.LastIndexOf('/') + 1);
                }
                else
                {
                    articleId = filterContext.HttpContext.Request.QueryString[ParamName];
                }
            }
            else
            {
                articleId = filterContext.HttpContext.Request.Form[ParamName];
            }
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated || String.IsNullOrEmpty(articleId) || articleId.ToInt32(-1) == -1)
            {
                filterContext.HttpContext.Response.Redirect("/workspace/home/index");
                return;
            }
            string loginName = filterContext.HttpContext.User.Identity.Name;
            var member = new User().Find(loginName);
            if (member == null)
            {
                filterContext.HttpContext.Response.Redirect("/workspace/home/forbidden");
                return;
            }
            if (member.SystemRole != Infrastructure.Entities.Enum.UserRole.Admin)
            {
                var article = new Article().Find(articleId.ToString());
                if (article == null)
                {
                    filterContext.HttpContext.Response.Redirect("/workspace/home/index");
                    return;
                }
                if (article.UserName != loginName)
                {
                    if (member.SystemRole == Infrastructure.Entities.Enum.UserRole.JounalsAdmin ||
                        member.SystemRole == Infrastructure.Entities.Enum.UserRole.MagazineAdmin)
                    {
                        if (!new Periodical().Any(c => c.Admin == loginName && c.Id == article.Peri_Id))
                        {
                            filterContext.HttpContext.Response.Redirect("/workspace/home/forbidden");
                        }
                    }
                    else if(member.SystemRole == Infrastructure.Entities.Enum.UserRole.ConferenceAdmin)
                    {
                        if (!new Meeting().Any(c => c.Admin == loginName && c.Id == article.Conference_Id))
                        {
                            filterContext.HttpContext.Response.Redirect("/workspace/home/forbidden");
                        }
                    }
                    else
                    {
                        filterContext.HttpContext.Response.Redirect("/workspace/home/forbidden");
                    }
                }
            }
        }
    }
}
